package hmi.canvas;

import hmi.View;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JComponent;

public class CanvasView extends View<Canvas> {
//
// The canvas view doesn't do anything special. But allows us to draw things in a special manner on the screen
//
	private CanvasMouseListener			mouseListener = new CanvasMouseListener(); 
	private CanvasMouseMotionListener	mouseMotionListener = new CanvasMouseMotionListener(); 
	private CanvasMouseWheelListener	mouseWheelListener = new CanvasMouseWheelListener();
	private CanvasComponentListener		componentListener = new CanvasComponentListener();
	private CanvasKeyListener			keyListener = new CanvasKeyListener();

	private	boolean		isDebug = false;
	
	private void println(String S) {
		if (isDebug) {
			System.out.println(S);
		}
	}
	
	@Override
	public void addListeners(JComponent component) {
		component.addMouseListener(mouseListener);
		component.addMouseMotionListener(mouseMotionListener);
		component.addMouseWheelListener(mouseWheelListener);
		component.addComponentListener(componentListener);
		component.addKeyListener(keyListener);
	}
	@Override
	public void removeListeners(JComponent component) {
		component.removeMouseListener(mouseListener);
		component.removeMouseMotionListener(mouseMotionListener);
		component.removeMouseWheelListener(mouseWheelListener);
		component.removeComponentListener(componentListener);
		component.removeKeyListener(keyListener);
	}

	public class CanvasMouseListener implements MouseListener {
		@Override
		public void mouseClicked(MouseEvent e) {
			println("mouseClicked");
			getModel().mScreenPos = e.getPoint();
		}

		@Override
		public void mousePressed(MouseEvent e) {
			println("mousePressed");
			getModel().mScreenPos = e.getPoint();
			getModel().mScreenDown.put(e.getButton(), getModel().mScreenPos);
			getModel().mPressed.put(e.getButton(), true);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			println("mouseReleased");
			getModel().mScreenPos = e.getPoint();
			getModel().mScreenUp.put(e.getButton(), getModel().mScreenPos);
			getModel().mPressed.put(e.getButton(), false);
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			println("mouseEntered");
			getModel().mScreenPos = e.getPoint();
		}

		@Override
		public void mouseExited(MouseEvent e) {
			println("mouseExited");
			getModel().mScreenPos = e.getPoint();		
		}
	
	}
	public class CanvasMouseMotionListener implements MouseMotionListener {

		@Override
		public void mouseDragged(MouseEvent e) {
			println("mouseDragged");
			getModel().mScreenPos = e.getPoint();
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			println("mouseMoved");
			getModel().mScreenPos = e.getPoint();
		}


	}
	public class CanvasMouseWheelListener implements MouseWheelListener {

		@Override
		public void mouseWheelMoved(MouseWheelEvent e) {
			println("mouseWheelMoved");
			getModel().mScreenPos = e.getPoint();
		}

	}
	public class CanvasKeyListener implements KeyListener {

		@Override
		public void keyTyped(KeyEvent e) {
			println("keyTyped");
			println("   code = " + e.getKeyCode());
			println("   char = " + e.getKeyChar());
		}

		@Override
		public void keyReleased(KeyEvent e) {
			println("keyReleased");
			println("   code = " + e.getKeyCode());
			println("   char = " + e.getKeyChar());
		}

		@Override
		public void keyPressed(KeyEvent e) {
			println("keyPressed");
			println("   code = " + e.getKeyCode());
			println("   char = " + e.getKeyChar());
		}
	}
	public class CanvasComponentListener implements ComponentListener {

		@Override
		public void componentResized(ComponentEvent e) {
			println("componentResized");
			refresh();		
		}

		@Override
		public void componentMoved(ComponentEvent e) {
			println("componentMoved");
			refresh();		
		}

		@Override
		public void componentShown(ComponentEvent e) {
			println("componentShown");
			refresh();		
		}

		@Override
		public void componentHidden(ComponentEvent e) {
			println("componentHidden");
			refresh();		
		}

	}

	private void refresh() {
		getComponent().paintImmediately(getComponent().getVisibleRect());
	}
}
